//
//  CateView.m
//  小团购
//
//  Created by 樊荣海 on 15/1/26.
//  Copyright (c) 2015年 樊荣海. All rights reserved.
//

#import "CateView.h"
#define btnW 83.4
#define btnH 50

@implementation CateView {


    UIButton *_currentBtn;
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self == [super initWithFrame:frame]) {
        
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        self.image = [UIImage resizedImage:@"bg_subfilter_other"];
        self.clipsToBounds = YES;
        self.userInteractionEnabled = YES;
    }
    
    return self;
    
}

- (void)setTitleArray:(NSArray *)titleArray {
    
    _titleArray = titleArray;
    
//    NSLog(@"_titleArray: %ld",_titleArray.count);
    
    //多少列
    int col = self.frame.size.width/btnW;
    
    NSInteger conut = _titleArray.count;
    
    for (int i = 0; i< conut; i++) {
        
        UIButton *btn = nil;
        if (i >= self.subviews.count) {
            //创建新的按钮
            btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn.titleLabel setFont:[UIFont systemFontOfSize:14]];
            [btn setTitleColor:globalTintColor forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [btn setBackgroundImage:[UIImage resizedImage:@"selected_bg"] forState:UIControlStateSelected];
            [self addSubview:btn];
            
        } else {
            //使用旧的按钮
            btn = self.subviews[i];
        }
        
        btn.hidden = NO;
        [btn setTitle:_titleArray[i] forState:UIControlStateNormal];
        btn.selected = [[[DBTool sharedDBTool] selectSubTitle:TabelTypeCate] isEqualToString:[btn titleForState:UIControlStateNormal]];
        
        if (btn.selected) {
            _currentBtn = btn;
        }
        
        //位置
        btn.frame = CGRectMake(i%col * btnW, i/col *btnH, btnW, btnH);
    }
    
    //如果过多, 隐藏多余的
    for (NSInteger i=conut; i < self.subviews.count; i++) {
        
        UIButton *btn = self.subviews[i];
        btn.hidden = YES;
    }
    
    [self layoutSubviews];
}

- (void)btnClick:(UIButton *)sender {
    
    _currentBtn.selected = NO;
    sender.selected = YES;
    _currentBtn = sender;
    
    
    if (_block) {
        _block([sender titleForState:UIControlStateNormal]);
    }

}

//显示 动画
- (void)showAnimation {
    
    [self layoutSubviews];
    
    self.transform = CGAffineTransformMakeTranslation(0, -self.frame.size.height);
    [UIView animateWithDuration:0.5 animations:^{
        
        self.transform = CGAffineTransformIdentity;
        
    } completion:^(BOOL finished) {
        
    }];
    
}

//隐藏 动画
- (void)hideAniamtion {
    
    [UIView animateWithDuration:0.5 animations:^{
        
        self.transform = CGAffineTransformMakeTranslation(0, -self.frame.size.height);
        
    } completion:^(BOOL finished) {
        
        self.transform = CGAffineTransformIdentity;
        
        [self removeFromSuperview];
        
    }];
}

- (void)layoutSubviews {
    
//    NSLog(@"layoutSubviews");
    
    [super layoutSubviews];
    
    //多少列
    int col = self.frame.size.width/btnW;
    
    for (int i = 0; i< _titleArray.count; i++) {
        
        UIButton *btn = self.subviews[i];
        
        //位置
        btn.frame = CGRectMake(i%col * btnW, i/col *btnH, btnW, btnH);
    }
    
    
    NSInteger rows = (_titleArray.count - 1) / col + 1;
    CGRect frame = self.frame;
    frame.size.height = rows * btnH +3;
    self.frame = frame;

}





















@end
